package leetcode_900;

import java.util.List;

/**
 *@author 周杨
 *KeysAndRooms_841 典型的深度搜索问题
 *describe:AC 91%
 *2018年11月1日 下午3:30:47
 */
public class KeysAndRooms_841 {
	boolean visited[];
	int count=0;
	public boolean canVisitAllRooms(List<List<Integer>> rooms) {
	      this.visited=new boolean[rooms.size()];
	      dfs(rooms,0);
	      return count==visited.length;
	}
	
	private void dfs(List<List<Integer>> rooms,int now) {
		if(visited[now])
			return ;
		visited[now]=true;
		++count;
		if(count==visited.length)
			return ;
		List<Integer> keys=rooms.get(now);
		if(keys==null||keys.size()==0)
			return ;
		for(int key:keys) {
			dfs(rooms,key);
		}
	}
}
